<div class="page-breadcrumbs">
    <ul class="breadcrumb">
        <li>
            <i class="fa fa-home"></i>
            <a href="#">Home</a>
        </li>
        <li>
            <a href="#">CMS管理</a>
        </li>
        <li>
            <a href="#!cms/col/col-index.html">联系方式</a>
        </li>
        <li class="active">联系方式修改</li>
    </ul>
</div>
<!-- Page Body -->
<div class="page-body">
    <div class="row">
        <div class="col-xs-12 col-md-12">
            <div class="widget">
                <div class="widget-header bordered-bottom bordered-themeprimary">
                    <i class="widget-icon fa fa-tasks themeprimary"></i>
                    <span class="widget-caption themeprimary">网站栏目基本信息</span>
                </div>
                <div class="widget-body">
                    <div class="form-horizontal bv-form" novalidate="novalidate">
                        <form id="editForm">
                            <input type="hidden" id="weibsite" name="weibsite"/>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">栏目名称</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="name" data-bv-field="name"><i class="form-control-feedback" data-bv-field="name" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">访问地址</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="url" data-bv-field="url"><i class="form-control-feedback" data-bv-field="url" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">上级栏目</label>
                                <div class="col-lg-9">
                                    <input type="hidden" class="form-control" id="parentId" name="parentId">
                                    <input type="text" class="form-control" id="parentName" name="parentName" onclick="javascript:colParent();">
                                </div>
                            </div>
                        </form>
                        <div class="form-group">
                            <div class="col-lg-8 col-lg-offset-4">
                                <button class="btn btn-blue" id="saveUpdate">保存</button>
                                <button class="btn btn-default" id="cancel">取消</button>
                            </div>
                        </div>
                        <div/>
                    </div>
                </div>
            </div>
        </div>

        <div id="colParentDialog" style="display:none;">
            <div class="row" style="margin: 15px;">
                <div class="col-md-12">
                    <ul id="colTree" class="row ztree"></ul>
                </div>
            </div>
        </div>

    </div>
<script type="application/javascript">
    var selectTreeNode = null;
    var setting = {
        async: {
            enable: true,
            url:"rest/v1/cms/col/"+$('#weibsite').val()+"/childs",
            autoParam:["id=parenId"],
            dataType: "json",
            type: "get",
            dataFilter: filter
        },
        callback: {
            beforeClick: beforeClick
        }
    };

    function filter(treeId, parentNode, childNodes) {
        if (!childNodes) return null;
        for (var i=0, l=childNodes.length; i<l; i++) {
            childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
        }
        return childNodes;
    }

    function beforeClick(treeId, treeNode) {
        selectTreeNode = treeNode;
    }

    $(function(){
        $.ajax({
            type : "get",
            url : "rest/v1/cms/col/" + window.atob(GetQueryString('target')),
            success: function (response) {
                formLoadJson(response.result);
            },
            error: function(error) {
                var obj = jQuery.parseJSON(error.responseText);
                Notify(obj.message, 'top-right', '800', 'warning', 'fa-bolt', true);
            }
        });

        $('#saveUpdate').click(function(e){
            var data = $('#editForm').data('bootstrapValidator');
            if (data) {
                data.validate();
                if (data.isValid()) {
                    $.ajax({
                        type: 'PUT',
                        url : "rest/v1/cms/col/" + window.atob(GetQueryString('target')),
                        contentType: 'application/json',
                        data: JSON.stringify($('#editForm').serializeJson()),
                        dataType: 'json',
                        success: function (response) {
                            Notify(response.message, 'top-right', '800', 'success', 'fa-bolt', true);
                            window.history.go(-1);
                        },
                        error: function(error) {
                            var obj = jQuery.parseJSON(error.responseText);
                            Notify(obj.message, 'top-right', '800', 'warning', 'fa-bolt', true);
                        }
                    });
                }
            }
        });

        $('#cancel').click(function(e){
            window.history.go(-1);
        });

        $('#editForm').bootstrapValidator({
            excluded: [':disabled'],
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: '栏目名称不能为空'
                        }
                    }
                },
                url: {
                    validators: {
                        notEmpty: {
                            message: '栏目地址不能为空'
                        }
                    }
                }
            }
        });
    });

    function colParent(){
        setting.async.url="rest/v1/cms/col/"+$('#weibsite').val()+"/childs";
        $.fn.zTree.init($("#colTree"), setting);
        layer.open({
            area: ['500px', '650px'],
            type: 1,
            title:'选择上级栏目',
            content: $('#colParentDialog'),
            btn: ['选择','取消'],
            yes: function(index, layero){
                if(null != selectTreeNode){
                    $('#parentId').val(selectTreeNode.id);
                    $('#parentName').val(selectTreeNode.name);
                    layer.close(index);
                }else{
                    Notify('请选择栏目', 'top-right', '800', 'warning', 'fa-bolt', true);
                }
            },cancel: function(){
                layer.close(index);
            }
        });
    }
</script>